package com.intelligent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.pojo.Process;
import com.intelligent.service.ProcessService;
import com.intelligent.mapper.ProcessMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author lenovo
* @description 针对表【process(工序信息表)】的数据库操作Service实现
* @createDate 2025-08-29 10:43:54
*/
@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper, Process>
    implements ProcessService{

    @Override
    public IPage<Map<String, Object>> getProcessPage(Page<Map<String, Object>> page,
                                                    String processCode,
                                                    String processName,
                                                    Integer processType,
                                                    Integer status) {
        return baseMapper.selectProcessPage(page, processCode, processName, processType, status);
    }

    @Override
    public boolean addProcess(Process process) {
        String processCode = generateProcessCode();
        process.setProcessCode(processCode);

        if(process.getStatus() == null){
            process.setStatus(0);
        }

        int result = baseMapper.insertProcess(process);
        return result > 0;
    }
    /**
     * 自动生成BOM编号
     * 格式：BOM + 年月日 + 4位序号，例如：BOM202501010001
     *
     * @return 生成的BOM编号
     */
    private String generateProcessCode() {
        // 获取当前日期
        String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        // 获取当前日期下的最大编号
        String maxBomCode = baseMapper.getMaxProcessCode();

        int sequence = 1;
        if (maxBomCode != null && maxBomCode.startsWith("GX" + currentDate)) {
            // 提取序号部分
            String sequenceStr = maxBomCode.substring(11); // BOM20250101 + 4位序号
            try {
                sequence = Integer.parseInt(sequenceStr) + 1;
            } catch (NumberFormatException e) {
                sequence = 1;
            }
        }

        // 格式化序号为4位数字
        String formattedSequence = String.format("%04d", sequence);

        return "BOM" + currentDate + formattedSequence;
    }
    @Override
    public boolean updateProcess(Process process) {
        // 验证工序ID是否存在
        if (process.getProcessId() == null) {
            throw new IllegalArgumentException("工序ID不能为空");
        }

        // 设置默认值（如果未提供）
        if (process.getProcessType() == null) {
            process.setProcessType(0); // 默认自制
        }
        if (process.getStatus() == null) {
            process.setStatus(0); // 默认启用
        }

        return baseMapper.updateProcess(process) > 0;
    }

    @Override
    public boolean deleteProcess(Long processId) {
        // 验证工序ID是否存在
        if (processId == null) {
            throw new IllegalArgumentException("工序ID不能为空");
        }

        // 检查工序是否存在
        Process existingProcess = baseMapper.selectById(processId);
        if (existingProcess == null) {
            throw new IllegalArgumentException("工序不存在，无法删除");
        }

        return baseMapper.deleteProcess(processId) > 0;
    }

    @Override
    public Map<String, Object> getProcessById(Long processId) {
        // 验证工序ID是否存在
        if (processId == null) {
            throw new IllegalArgumentException("工序ID不能为空");
        }

        // 查询工序详细信息
        Map<String, Object> processInfo = baseMapper.selectProcessById(processId);
        if (processInfo == null) {
            throw new IllegalArgumentException("工序不存在");
        }

        return processInfo;
    }
}
